package com.barradata.common.view


import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import com.barradata.R
import com.barradata.common.utils.EditTextUtils
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import java.util.concurrent.TimeUnit

/**
 * Created by WuNan-QF on 16/7/27.
 */
abstract class BottomPopupFragment<T> : DialogFragment() {

    var compositeDisposable = CompositeDisposable()

    var callbackAction: ((T) -> Unit)? = null

    protected abstract val layoutId: Int

    protected abstract fun initView()
    //仅统一函数名称，不强制调用
    protected fun fetchData() {}

    fun show(context: AppCompatActivity) {
        EditTextUtils.hideKeyboard(context)
        val disposable = Observable.timer(100, TimeUnit.MILLISECONDS)
                .subscribe { super.show(context.supportFragmentManager, this.toString()) }
        compositeDisposable.add(disposable)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val params = dialog.window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.gravity = Gravity.BOTTOM
        params.windowAnimations = R.style.bottom_popup_window_anim
        dialog.window!!.attributes = params
        dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))//注意此处


    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.window!!.requestFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(layoutId, container)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    override fun onDestroy() {
        compositeDisposable.dispose()
        super.onDestroy()
    }

}
